<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/21 15:02
 * Remark:
 */

namespace App\Func;

use App\Consts\RedisConsts\RedisConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Http\pkConfig\login\PkLoginConfigGet;
use App\Models\Staff\StaffDepartmentModel;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Redis;

class TokenFuc
{
    /**
     * 获取 requestId
     * @return string
     */
    public static function getRequestId() : string{
        // 获取并删除请求ID验证器
        return request()->requestId;
    }

    /**
     * 获取验证器是否存在
     * @return bool
     */
    public static function verifyRequestId() : bool {
        $requestId = RequestFunc::getHeaderParams("Transfer-Toc");
        //验证当前验证器是否存在
        $isExist = Redis::exists($requestId);
        if($isExist){
            // 这里是因为以前有一个验证器模拟了微信的每一次请求都会校验
            // Redis::del($requestId);
            return true;
        }
        return false;
    }

    public static function getCurrentStaffDepartment($isGetMaster = false){
        $staffId = self::getCurrentStaffInfo()['staff_id'];
        $model = new StaffDepartmentModel();
        $departmentList = $model->getStaffDepartments($staffId);
        if(!$departmentList){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                null,
                '登录失效,请重新登录1',
                401,
            ));
        }
        $departmentList = $departmentList->toArray();
        $formatDepartmentList = array_column($departmentList,null,'is_master');
        if($isGetMaster){
            return $formatDepartmentList[1];
        }
        return $departmentList;
    }

    /**
     * 获取当前登录用户信息
     * @return array|mixed
     */
    public static function getCurrentStaffInfo(): mixed
    {
//        dd(debug_print_backtrace());
//        $requestId = RequestFunc::getHeaderParams("Transfer-Toc");

        $requestId = RequestFunc::getHeaderParams("U-TK");
        // 解密token
        // 通过Rsa解密获取redis中正确的key
        $encryptionFunc = new EncryptionFunc();
        $adminLoginPkConfig = new PkLoginConfigGet();
        $redisKey = $encryptionFunc->rsaToBase64Decode($adminLoginPkConfig->getPrKey(),$requestId);

        $isExist = Redis::exists($redisKey);
        // 更新Redis过期时间
        $req = Redis::expire($redisKey, 3600);
//        dd(unserialize(Redis::get($redisKey)));
        if($isExist){
            return unserialize(Redis::get($redisKey));
        }else{
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                null,
                '登录失效,请重新登录',
                401,
            ));
        }

    }

    public static function getPermitPath(){
        return [
            'api/login',
            'api/downPaperLetter',
            'api/meCallBack',
            'api/tt',
            'api/displayFile',
            'api/tocCs',
            'api/companySelectByLogin',
            'api/test',
            'api/currentStaff',
            'api/dictionary',
            'api/loginDepartment',
            'api/dingTalkScanQRCodesLogin',
            'api/logout',
            'api/triggerDingTalkRollBack',
            'api/triggerDingTalkRollBackEvent',
            'api/downloadFile',
            'api/registerCallBackInput',
            'api/importExcel',

			# by BREEZEER @2023/4/19
			#
			#
			'company.x/map_company_to_database_config'				,
			'import/mapExcelFileColumnHeaderToDatabaseTableField'	,
			'import/lawCaseTableFieldList'							,
            'api/sipCallback',
            # new
            'api/v2/login',
            'api/getPukey',
            'api/v2/staffDepartmentSelect',
            'api/v2/switchStaffLogin',
            'api/lawCaseStatus',
            'api/staffPermission/staffOwnAllNavigationMenu'
            #


        ];
    }
}